using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// 违章记录
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class CBS_BreakRulesController : CoreApiController
    {
        private readonly CBS_BreakRulesService _service;
        private readonly CoreDBContext _coreDBContext;
        /// <summary>
        /// CBS_BreakRules
        ///</summary>
        public CBS_BreakRulesController(CBS_BreakRulesService service, CoreDBContext coreDBContext)
        {
            _service = service;
            _coreDBContext = coreDBContext;
        }

        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<CBS_BreakRulesDto> pagingQueryModel)
        {
            IPageModel<CBS_BreakRulesDto> result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Id,
                a.ViolationClause,//违规条款
                a.DiscoveringPeopleID,//发现人ID
                a.DiscoveringPeople,//发现人
                a.ViolationDate,//违规日期
                a.ViolationInstructions,//违规情况说明
                a.UpdateTime,//更新时间
                a.UpdateUserText,//更新人
                CBS_BreakRulesAttachment = a.CBS_BreakRulesAttachment.Where(att => att.Type == "1")//附件 0违章图片1签名
            }).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(CBS_BreakRulesDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess();
            }
            else
            {
                if (dto.CBS_BreakRulesAttachment != null && dto.CBS_BreakRulesAttachment.Any())
                {
                    //因App接口没有 附件上传功能  所以能传过来的都是签名，在这里先给签名图片删除， 在重新添加
                    var breakRulesAttachmentList = _coreDBContext.CBS_BreakRulesAttachment.Where(a => a.BreakRulesId == dto.Id && a.Type == "1");
                    _coreDBContext.CBS_BreakRulesAttachment.RemoveRange(breakRulesAttachmentList);
                    dto.CBS_BreakRulesAttachment.ForEach(a =>
                    {
                        a.BreakRulesId = dto.Id;
                    });
                    _coreDBContext.CBS_BreakRulesAttachment.AddRange(dto.CBS_BreakRulesAttachment);
                }
                await _service.UpdateAsync(dto, true, noUpdateProperties: new System.Collections.Generic.List<string>() {
                    //nameof(dto.ProcessingResult),//处理结果
                    nameof(dto.ExamineState),//审批结果
                    nameof(dto.ExamineUser),//审批用户
                    nameof(dto.CreateFactory),//隶属工厂
                    nameof(dto.CreateDept),//添加部门
                    nameof(dto.UpdateDept)//更改部门
                });
                return this.JsonSuccess();
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {
            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var retObj = new
            {
                model.CBSListId,//承包商名录ID
                model.ViolatorsID,//违规人ID
                model.ViolationDate,//违规日期
                model.DiscoveringPeopleID,//发现人ID
                model.ViolationClause,//违规条款
                model.ViolationInstructions,//违规情况说明
                model.Remarks,//备注
                model.ProcessingOpinion,//处理意见
                model.ContractorQZ,//承包商违章签字
                model.ProcessingResult,//处理结果
                CBS_BreakRulesAttachment = model.CBS_BreakRulesAttachment.Where(a => a.Type == "1")
                //model.ExamineState,//审批结果
                //model.ExamineUser,//审批用户
                //model.CreateFactory,//隶属工厂
                //model.CreateDept,//添加部门
                //model.UpdateDept,//更改部门
            };
            return this.JsonSuccess(retObj);
        }



    }
}
